/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: MD5.java 
 * @Prject: carfactory
 * @Package: com.zizo.util 
 * @Description: TODO
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月22日 下午6:54:49 
 * @version: V1.0   
 */
package com.zizo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: MD5
 * @Description: TODO
 * @author: Alexis
 * @date: 2016年7月22日 下午6:54:49
 */
public class MD5 {
	/**
	 * MD5 16bit Encrypt Methods.
	 * 
	 * @param readyEncryptStr
	 *            ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException
	 */
	public static final String MD5_16bit(String readyEncryptStr) {
		if (readyEncryptStr != null) {
			return MD5.MD5_32bit(readyEncryptStr).substring(8, 24);
		} else {
			return "";
		}
	}

	/**
	 * MD5 32bit Encrypt Methods.
	 * 
	 * @param readyEncryptStr
	 *            ready encrypt string
	 * @return String encrypt result string
	 * @throws NoSuchAlgorithmException
	 */
	public static final String MD5_32bit(String readyEncryptStr) {
		if (readyEncryptStr != null) {
			MessageDigest md = null;
			try {
				md = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			md.update(readyEncryptStr.getBytes());
			byte[] b = md.digest();
			StringBuilder su = new StringBuilder();
			for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
				String haxHex = Integer.toHexString(b[offset] & 0xFF);
				if (haxHex.length() < 2) {
					su.append("0");
				}
				su.append(haxHex);
			}
			return su.toString();
		} else {
			return null;
		}
	}

	public static void main(String[] args) {
		System.out.println(MD5.MD5_16bit("heyertreeertrewtwreterqwytuteyurityuteyueryretyw4tq4rewrfsgdfgfdgan"));
	}
}
